package com.redball;

import java.util.Random;

import android.graphics.Bitmap;
import android.view.MotionEvent;

public class RedBall extends GraphicObject {
	
	public boolean _isTouched;
	private Panel _panel;
	private double _deltaX = 0;
	private double _deltaY = 0;
	private final double BOUNCE_FACTOR = 0.5;
	private final int POSITION_TESTING_FACTOR = 10;
	public final double FRICTION_FACTOR;
	private int _savePosition = 0;
	private int _oldX = 0;
	private int _oldY = 0;
	
	public RedBall(Panel panel, Bitmap bitmap, int x, int y) {
		super(bitmap, x, y);
		FRICTION_FACTOR = 0.3 + (new Random().nextInt(5)/10.0);
		this._panel = panel;
	}

	public void updatePhysics(){
		
		GraphicObject.Coordinates coord;
		coord = getCoordinates();

		if (!_isTouched)
		{
			// Setting X
			_deltaY += _panel.yVector * _panel.GRAVITY_FACTOR * _panel.SLOW_MOTION_FACTOR;
			_deltaX += _panel.xVector * _panel.GRAVITY_FACTOR * _panel.SLOW_MOTION_FACTOR;
			coord.setX(coord.getX() + (int) (_deltaX * _panel.SLOW_MOTION_FACTOR));
			coord.setY(coord.getY() + (int) (_deltaY * _panel.SLOW_MOTION_FACTOR));
		}

		// Borders X
		if (coord.getX() / 2 < 0)
		{
			coord.setX(0);
			angleChange(0);
			_deltaX -= BOUNCE_FACTOR;
		} else if (coord.getX() + getGraphic().getWidth() > _panel.getWidth())
		{
			coord.setX(_panel.getWidth() - getGraphic().getWidth());
			angleChange(Math.PI);
			_deltaX += BOUNCE_FACTOR;
		}

		// Borders Y
		if (coord.getY() / 2 < 0)
		{
			coord.setY(0);
			angleChange(-Math.PI / 2);
		} else if (coord.getY() + getGraphic().getHeight() > _panel.getHeight())
		{
			coord.setY(_panel.getHeight() - getGraphic().getHeight());
			angleChange(Math.PI / 2);
			_deltaY += BOUNCE_FACTOR;
			if (_deltaX != 0)
			{
				_deltaX += (_deltaX > 0) ? -FRICTION_FACTOR : FRICTION_FACTOR;
			}
		}
		
	}
	public void handleColission(double normal){
		angleChange(normal);
	}
	
	public void handleObjectColission(Coordinates coordinates) {
		Coordinates coord = coordinates;
		int dy = getCoordinates().getY()-coord.getY();
		int dx = getCoordinates().getX() - coord.getX();
		double angle = Math.atan2(dy,dx);
		
		handleColission(angle);
		
	}
	
	public boolean onTouchEvent(MotionEvent event)
	{
//			if (_onChangeGravity) {
//				_gravitySelector.onTouchEvent(event);
//			}
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
					int TOUCH_FACTOR = 20;
					if (event.getX() > getCoordinates().getX() - TOUCH_FACTOR
							&& event.getX() < getCoordinates().getX() + getGraphic().getWidth() + TOUCH_FACTOR
							&& event.getY() > getCoordinates().getY()  - TOUCH_FACTOR
							&& event.getY() < getCoordinates().getY() + getGraphic().getHeight() + TOUCH_FACTOR)
					{
						_isTouched = true;
					}
			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				// Check if it's on the ball
				if (_isTouched)
				{
					// stop Pause mode if already enabled
					_panel.togglePause(false);
					if (_panel.wall.checkIfHit(getCoordinates().getX(), getCoordinates().getY(),
							getGraphic().getWidth() / 2) == null)
					{
						getCoordinates().setX(
								(int) (event.getX() - getGraphic().getWidth() / 2));
						getCoordinates().setY(
								(int) (event.getY() - getGraphic().getHeight() / 2));

					} else 	{
						_isTouched = false;
					}

					// Saving it's current position
					if (_savePosition == 0)
					{
						_oldX = (int) (getCoordinates().getX() + getGraphic().getWidth() / 2);
						_oldY = (int) (getCoordinates().getY() + getGraphic().getHeight() / 2);

						_savePosition = POSITION_TESTING_FACTOR;
					} else
					{
						--_savePosition;
					}
				}
			} else if (event.getAction() == MotionEvent.ACTION_UP)
			{
//				if (_onChangeGravity) {
//					double gravityDegree = getGravityAngle();
//					
//					xVector = (float)(Math.cos(gravityDegree) * GRAVITY_FACTOR * SLOW_MOTION_FACTOR);
//					yVector = (float)(Math.sin(gravityDegree) * GRAVITY_FACTOR * SLOW_MOTION_FACTOR);
//					_onChangeGravity = false;
//				}
				if (_isTouched)
				{
					// Sets its speed and direction
					_isTouched = false;

					_deltaX = (int) 2 * (event.getX() - _oldX) / (POSITION_TESTING_FACTOR);
					_deltaY = (int) 2 * (event.getY() - _oldY) / (POSITION_TESTING_FACTOR);
				}
			}
			return true;
		}
	public void angleChange(double normal)
	{

		double tmpX = _deltaX;
		double tmpY = _deltaY;
		double ballAngle = Math.atan2(tmpY, tmpX);
		double deltaAngle = Math.PI - 2 * (ballAngle + normal);

		_deltaX = tmpX * Math.cos(deltaAngle) - tmpY * Math.sin(deltaAngle);
		_deltaY = (tmpX * Math.sin(deltaAngle) + tmpY * Math.cos(deltaAngle));
	}
}
